package com.sunchangming.rpcgen;

import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class MyEnumConverter implements Converter {

	@SuppressWarnings("rawtypes")
	@Override
	public boolean canConvert(Class clazz) {
		return clazz.equals(Enum.class);
	}

	@Override
	public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
		// TODO Auto-generated method stub

	}

	@Override
	public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
		final Enum ns = new Enum();
		ns.name = reader.getAttribute("name");
		while (reader.hasMoreChildren()) {
			reader.moveDown();
			try {
				if ("element".equals(reader.getNodeName())) {
					final EnumElement ns1 = (EnumElement) context.convertAnother(ns, EnumElement.class);
					ns1.parent = ns;
					ns.add(ns1);
				} else
					throw new ConversionException("不认识的节点" + reader.getNodeName());
			} finally {
				reader.moveUp();
			}
		}
		return ns;
	}

}
